<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
          @change="deptChange"
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停车场" prop="parkId">
          <el-select v-model="queryParams.parkId" placeholder="请选择" clearable >
            <el-option
              v-for="item in allParkList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      <el-form-item label="泊位编码" prop="berthCode">
        <el-input
          v-model="queryParams.berthCode"
          placeholder="请输入泊位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地磁编码" prop="magsensorCode">
        <el-input
          v-model="queryParams.magsensorCode"
          placeholder="请输入地磁编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报类型" prop="uploadType">
        <el-select v-model="queryParams.uploadType" placeholder="请选择上报类型" clearable>
          <el-option
            v-for="item in dict.type.EMagsensorUploadType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          type="monthrange"
          range-separator="至"
          :clearable="false"
          value-format="yyyy-MM"
          :picker-options="pickerWenti"
          start-placeholder="开始月份"
          end-placeholder="结束月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column 
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template 
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'magsensorManufacturer'" :options="dict.type.EMagsensorManufacture" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'uploadType'" :options="dict.type.EMagsensorUploadType" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'status'" :options="dict.type.EMagsensorUploadStatus" :value="row[item.prop]"/>
          <div v-else-if="item.prop == 'markImg' && row[item.prop]">
            <el-image
              style="width: 100px; height: 100px"
              :preview-src-list="[row[item.prop]]"
              :src="row[item.prop]"></el-image>
          </div>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {fetchList,} from "@/api/park/spmaguplog";
import { getDepartmentList,getListAuthPark } from '@/api/base'
import { timesToYyMmDd1,getDay } from "@/utils/time.js";
export default {
  name: "spmaguplog",
  dicts: ['EMagsensorManufacture','EMagsensorUploadType','EMagsensorUploadStatus'],
  components:{},
  data() {
    return {
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'片区名称',prop:'parkName',},
        {label:'泊位编码',prop:'berthCode',},
        {label:'地磁厂家',prop:'magsensorManufacturer',},
        {label:'地磁编码',prop:'magsensorCode',},
        {label:'地磁上报类型',prop:'uploadType',},
        {label:'状态变化计数',prop:'count',},
        {label:'地磁上报时间',prop:'uploadTime',},
        {label:'地磁上报状态',prop:'status',},
        {label:'标定图片',prop:'markImg',},
        {label:'标定人员',prop:'markBy',},
        {label:'创建时间',prop:'createTime',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        dateRange:[],
      },
      departmentList: [],//机构数据
      allParkList: [],//停车场数据
      pickerWenti:{
        disabledDate: (time) => {
          let startTime = '2024-04-01 00:00:00'
          return time.getTime() < new Date(startTime).getTime()
        }
      },
    };
  },
  created() {
    this.getDepartmentList();
    this.getList();
  },
  methods: {
    getTimeQujian(){//
      let dateRange = this.queryParams.dateRange || []
      if(!dateRange.length) {
        let nowDay = new Date()
        let month = nowDay.getMonth() + 1
        let year = nowDay.getFullYear()
        if(1 <= month && month <= 3) {
          this.queryParams.dateRange = [year + '-' + '01',year + '-' + '03']
          this.queryParams.date = year + '01' + '-' + year + '03'
        }else if(4 <= month && month <= 6){
          this.queryParams.dateRange = [year + '-' + '04',year + '-' + '06']
          this.queryParams.date = year + '04' + '-' + year + '06'
        }else if(7 <= month && month <= 9){
          this.queryParams.dateRange = [year + '-' + '07',year + '-' + '09']
          this.queryParams.date = year + '07' + '-' + year + '09'
        }else if(10 <= month && month <= 12){
          this.queryParams.dateRange = [year + '-' + '10',year + '-' + '12']
          this.queryParams.date = year + '10' + '-' + year + '12'
        }
        return false
      } else {
        let startTime = dateRange[0]
        let endTime = dateRange[1]
        let startList = startTime.split('-') || []
        let endList = endTime.split('-') || []
        let start = startList[1] || ''
        let startYear = startList[0] || ''
        let end = endList[1] || ''
        let endYear = endList[0] || ''
        this.queryParams.date = startYear + start + '-' + endYear + end
        let startflag = [1,4,7,10]
        let endflag = [3,6,9,12]
        let sFlag = startflag.find(item=>item == start)
        let eFlag = endflag.find(item=>item == end)
        console.log(sFlag,eFlag,startList)
        if(!sFlag || !eFlag) {
          this.$modal.msgWarning('开始时间月份必须是1月、4月、7月、10月并且结束时间月份必须是3月、6月、9月、12月,');
          return true
        }
        return false
      }
      
    },
    async deptChange(e){//机构下拉选择
      let queryParams = {...this.queryParams}
      queryParams.parkId = ''
      this.queryParams = {...queryParams}
      if(!e) return this.allParkList = []
      this.allParkList = await this.getListAuthPark(e)
    },
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async getListAuthPark(departmentId){//获取停车场
      let res = await getListAuthPark({departmentId,parkType:2})
      return res.data || []
    },
    /** 查询车位信息列表 */
    getList() {
      let flag = this.getTimeQujian()
      if(flag) return
      this.loading = true;
      let queryParams = {
        ...this.queryParams
      }
      if(queryParams.dateRange.length) {
        queryParams.createTimeRange = queryParams.date
      }
      delete queryParams.dateRange
      delete queryParams.date
      fetchList(queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        this.spparkplaceList = rows
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = []
      this.handleQuery();
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
